{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "4dcc8959-258e-4bb0-8f33-e9aab3d18f7c"
   },
   "source": [
    "## 导入工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "uuid": "75e0e426-cdce-4087-abab-0b2ca2bdb01a"
   },
   "outputs": [],
   "source": [
    "import ffmpeg\n",
    "import os\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "a9bf96d6-a93e-47b3-8142-67629a78d43e"
   },
   "source": [
    "## 视频转图片函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "uuid": "3c5dedfd-4765-49d7-9223-5beca87744dd"
   },
   "outputs": [],
   "source": [
    "def video2img(dir_video, dir_imgs):\n",
    "    stream = ffmpeg.input(dir_video)\n",
    "    stream = ffmpeg.output(stream, dir_imgs)\n",
    "    try:\n",
    "        ffmpeg.run(stream)\n",
    "    except ffmpeg.Error as e:\n",
    "        print('stdout:', e.stdout)\n",
    "        print('stderr:', e.stderr)\n",
    "        raise e"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "b15dbc5a-fbb1-4f15-bb10-df29350ecbef"
   },
   "source": [
    "## 高清视频转化为图片"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "1939fde1-0c6f-4183-8d0c-14124c4af51d"
   },
   "source": [
    "### 转化为１００张图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "uuid": "7d1c528a-88cb-4599-a3a8-73c5e0dbdd2e"
   },
   "outputs": [],
   "source": [
    "input_dir = './HIGH/'\n",
    "output_dir = './h_GT/'\n",
    "if not os.path.exists(output_dir):\n",
    "    os.mkdir(output_dir)\n",
    "list_files = os.listdir(input_dir)\n",
    "\n",
    "for i in range(len(list_files)):\n",
    "    file_name = list_files[i]\n",
    "    dir_video = input_dir + file_name\n",
    "    dir_out = output_dir + file_name[:-4] + '/'\n",
    "    if not os.path.exists(dir_out):\n",
    "        os.mkdir(dir_out)\n",
    "    dir_out = dir_out + '%3d.bmp'\n",
    "    video2img(dir_video, dir_out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "a38f2688-6119-4526-9c28-a4505816e524"
   },
   "source": [
    "## 低清视频转化为图片"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "7c4eac9f-4815-4dab-be3b-bc4ee17bd448"
   },
   "source": [
    "### 转化为１００张图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "uuid": "0e268d02-3bd6-4e19-821b-b12dd9e7cdbb"
   },
   "outputs": [],
   "source": [
    "input_dir = './LOW/'\n",
    "output_dir = './l/'\n",
    "if not os.path.exists(output_dir):\n",
    "    os.mkdir(output_dir)\n",
    "list_files = os.listdir(input_dir)\n",
    "\n",
    "for i in range(len(list_files)):\n",
    "    file_name = list_files[i]\n",
    "    dir_video = input_dir + file_name\n",
    "    dir_out = output_dir + file_name[:-4] + '/'\n",
    "    if not os.path.exists(dir_out):\n",
    "        os.mkdir(dir_out)\n",
    "    dir_out = dir_out + '%3d.bmp'\n",
    "    video2img(dir_video, dir_out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "f382b33a-e9ee-4ac6-b927-faf6b385923c"
   },
   "source": [
    "## 读取高清图片获取大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "uuid": "d66fdb97-c514-4b47-b96e-8bed30abc50a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1080, 1920, 3)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "path = \"./h_GT/Youku_00000_h_GT/001.bmp\"\n",
    "img = cv2.imread(path)\n",
    "img.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "17762f55-732c-4b22-ab37-77189bc53893"
   },
   "source": [
    "## 视频画图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "uuid": "c1c201f2-8c1c-4b4d-b654-234e16a65a7f"
   },
   "outputs": [],
   "source": [
    "# cv2.imshow(path,img)\n",
    "# cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "8f3b9143-6f02-4554-b182-a3a95e5eaa1d"
   },
   "source": [
    "## 读取低清图片获取大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "uuid": "fb86201f-0054-4e15-aa84-a40d7f1f28f0"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(270, 480, 3)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "path = \"./l/Youku_00000_l/001.bmp\"\n",
    "img = cv2.imread(path,1)\n",
    "img.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "59e7b6ef-b3bb-4c45-9b1f-8a8301c14e3c"
   },
   "source": [
    "## 视频画图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "uuid": "d58bc5de-ec20-429c-a6a3-61b4968121e7"
   },
   "outputs": [],
   "source": [
    "# cv2.imshow(path,img)\n",
    "# cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "580ddacf-5330-42c6-a160-0fcf0558d679"
   },
   "source": [
    "### 图片灰度处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "uuid": "546aa017-745b-40f4-aa5a-005e8aa078a8"
   },
   "outputs": [],
   "source": [
    "path = \"./h_GT/Youku_00000_h_GT/001.bmp\"\n",
    "img = cv2.imread(path)\n",
    "dst = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n",
    "# cv2.imshow('src', dst)\n",
    "# cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "0588a4ec-aae3-49a3-89ca-23a8131dcdb8"
   },
   "source": [
    "## 视频图片灰度处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "uuid": "55c06128-7653-4b09-8bf4-a87eae1d4508"
   },
   "outputs": [],
   "source": [
    "input_dir = './h_GT/Youku_00000_h_GT/'\n",
    "output_dir = './h_GT_gray/'\n",
    "if not os.path.exists(output_dir):\n",
    "    os.mkdir(output_dir)\n",
    "list_imgs = os.listdir(input_dir)\n",
    "\n",
    "for i in range(len(list_imgs)):\n",
    "    imgs_name = list_imgs[i]\n",
    "    img = cv2.imread(input_dir + imgs_name)\n",
    "    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n",
    "    cv2.imwrite(output_dir + imgs_name, gray)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "301bf150-5845-4fe7-8f6e-ce90be5aea4d"
   },
   "source": [
    "## 图片转视频函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "uuid": "7e035636-475e-46b5-8689-3d3dc583a677"
   },
   "outputs": [],
   "source": [
    "def imgs2dideo(imgs_dir, video_dir):\n",
    "    stream = ffmpeg.input(imgs_dir)\n",
    "    stream = ffmpeg.output(stream, video_dir, pix_fmt='yuv420p')\n",
    "    ffmpeg.run(stream)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "25b70e0c-5c8e-4bd9-beb5-266af48bb04b"
   },
   "source": [
    "## 视频灰度图片合成视频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "uuid": "914acc6e-1b71-44ac-86bd-305e28a6c8d1"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./h_GT_gray/\n"
     ]
    }
   ],
   "source": [
    "input_dir = './h_GT_gray/'\n",
    "output_dir = './h_GT_mv_gray/'\n",
    "if not os.path.exists(output_dir):\n",
    "    os.mkdir(output_dir)\n",
    "print(input_dir)\n",
    "list_imgs = os.listdir(input_dir)\n",
    "output_dir = output_dir + 'gray_h_GT_Res.y4m'\n",
    "imgs2dideo(input_dir+'/%3d.bmp' , output_dir)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "d38c71a4-cb0d-4916-a535-dcf171286346"
   },
   "source": [
    "## 高清图片转为视频"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "54a33455-e5e3-46af-b2e4-86b012d02ee4"
   },
   "source": [
    "### １００张图片转化为视频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "uuid": "dd6582b0-f4c9-42b4-bb20-e678edcefa76"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Youku_00000_h_GT\n"
     ]
    }
   ],
   "source": [
    "!ls ./h_GT/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "uuid": "9faaff43-ffe8-4d33-8752-5cf8c89c7fd2"
   },
   "outputs": [],
   "source": [
    "input_dir = './h_GT/'\n",
    "output_dir = './h_GT_mv/'\n",
    "if not os.path.exists(output_dir):\n",
    "    os.mkdir(output_dir)\n",
    "list_imgs = os.listdir(input_dir)\n",
    "\n",
    "for i in range(len(list_imgs)):\n",
    "    imgs_name = list_imgs[i]\n",
    "    if 'ipynb' not in imgs_name:\n",
    "        output_dir = output_dir + imgs_name[:-2] + '_h_GT_Res.y4m'\n",
    "        imgs2dideo(input_dir+imgs_name+'/%3d.bmp' , output_dir)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "fb96d6a5-13e6-4fa2-82ac-683df112a8c0"
   },
   "source": [
    "## 低清图片转为视频"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "uuid": "1b466437-1305-4c83-845c-20904437e3bc"
   },
   "source": [
    "### １００张图片转化为视频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "uuid": "592a969a-5253-419c-acdd-118a999a3b05"
   },
   "outputs": [],
   "source": [
    "input_dir = './l/'\n",
    "output_dir = './l_mv/'\n",
    "if not os.path.exists(output_dir):\n",
    "    os.mkdir(output_dir)\n",
    "list_imgs = os.listdir(input_dir)\n",
    "\n",
    "for i in range(len(list_imgs)):\n",
    "    imgs_name = list_imgs[i]\n",
    "    if 'ipynb' not in imgs_name:\n",
    "        output_dir = output_dir + imgs_name[:-2] + '_l_Res.y4m'\n",
    "        imgs2dideo(input_dir+imgs_name+'/%3d.bmp' , output_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "uuid": "d5729bca-cd1d-4d42-bc53-bbfa151ebd7d"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "tianchi_metadata": {
   "competitions": [],
   "datasets": [
    {
     "id": "112835",
     "title": "优酷"
    }
   ],
   "description": "",
   "notebookId": "281711",
   "source": "dsw"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
